package org.one.afternoon.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.one.afternoon.common.Result;
import org.one.afternoon.config.MinioConfig;
import org.one.afternoon.service.EdgeService;
import org.one.afternoon.utils.MinioUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping(value = "/edge")
@Slf4j
public class EdgeController {

    @Autowired
    private EdgeService edgeService;

    @CrossOrigin // 跨域访问
    @PostMapping("/upload_image")
    @ResponseBody
    public Result<String> upload(@RequestParam("image") MultipartFile image,
                                 @RequestParam("latitude") String latitude,
                                 @RequestParam("longitude") String longitude) {
        //文件名
        String fileName = image.getOriginalFilename();
        String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
        //类型
        String contentType = image.getContentType();
        try {
            edgeService.uploadBucket(image.getInputStream(), newFileName, contentType);
        } catch (IOException e) {
            log.error(e.getMessage());
        }
        edgeService.saveImage(newFileName, latitude, longitude);
        return Result.success("成功");
    }
}
